﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

using PasswordProvider.ViewModel.Properties;
using PasswordProvider.ViewModel.Framework;

namespace PasswordProvider.ViewModel
{
    /// <summary>
    /// View model for a custom message box.
    /// </summary>
    public class MessageBoxViewModel : VisualViewModel, IRequestClose
    {
        #region IRequestClose Members

        public event EventHandler CloseRequested;

        #endregion

        #region Construction

        internal MessageBoxViewModel(string heading, string message, MessageBoxButton buttons)
            : base(heading)
        {
            Message = message;
        
            switch (buttons)
            {
                case MessageBoxButton.OK:
                    CancelCommand.Visibility = Visibility.Collapsed;
                    OkCommand.IsDefault = true;
                    break;
                case MessageBoxButton.OKCancel:
                    CancelCommand.Visibility = Visibility.Visible;
                    CancelCommand.DisplayName = Strings.General_Cancel;
                    CancelCommand.IsDefault = true;
                    break;
                case MessageBoxButton.YesNo:
                    CancelCommand.Visibility = Visibility.Visible;
                    CancelCommand.DisplayName = "No";
                    OkCommand.DisplayName = "Yes";
                    CancelCommand.IsDefault = true;
                    break;
                default:
                    throw new InvalidOperationException(buttons.ToString() + " not supported");
            }
        }

        #endregion

        #region Public Properties

        public string Message
        {
            get;
            set;
        }

        public bool IsOk
        {
            get;
            set;
        }

        public CommandViewModel OkCommand
        {
            get
            {
                if (_okCommand == null)
                {
                    _okCommand = new CommandViewModel(
                        Strings.General_Ok,
                        new RelayCommand(param => OkCommandExecuted()));
                }
                return _okCommand;
            }
        } private CommandViewModel _okCommand;


        public CommandViewModel CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new CommandViewModel(
                        Strings.General_Cancel,
                        new RelayCommand(param => CancelCommandExecuted()));
                }
                return _cancelCommand;
            }
        } private CommandViewModel _cancelCommand;

        #endregion

        #region Private

        private void CancelCommandExecuted()
        {
            IsOk = false;
            OnCloseRequested();
        }

        private void OkCommandExecuted()
        {
            IsOk = true;
            OnCloseRequested();
        }

        private void OnCloseRequested()
        {
            if(CloseRequested != null)
                CloseRequested(this, new EventArgs());
        }

        #endregion
    }
}
